/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package tank;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import tank.controllers.SoundPlayer;
import tank.controllers.TankPlayController;
import tank.controllers.TankSelectController;
import tank.models.GameSettings;
import tank.models.Global;
import tank.models.Map;
import tank.views.TankPlayView;
import tank.views.TankSelectView;
/**
 *
 * @author shengchang
 */
public class TankApp {
    
    //Tank application start point
    public static void main(String[] args)
    {
        try {
            //init Global data
            initGlobalData();
        } catch (IOException ex) {
            Logger.getLogger(TankApp.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        //display Tank window
        //Create one Frame
        JFrame frame = new JFrame();
        //Create controller
        TankSelectController selectController = new TankSelectController();
        TankPlayController playController = new TankPlayController();
        //Create view
        TankSelectView selectView = new TankSelectView(selectController);
        TankPlayView playView = new TankPlayView(playController);
        
        //add keypad listener
        frame.addKeyListener(playController);
        frame.addMouseListener(playController);
        frame.addMouseMotionListener(playController);

        //build releation ship of controller, view and model
        selectController.setView(selectView);
        selectController.setFrame(frame);
        Global.viewList.add(selectView);
        playController.setView(playView);
        playController.setFrame(frame);
        Global.viewList.add(playView);
        
        //Display tank window
        frame.setTitle("Tank Game V1.0");
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        selectView.setPreferredSize(new Dimension(800, 600));
        frame.add(selectView);
        frame.pack();
        frame.setVisible(true);

        GameSettings.viewStartX = frame.getInsets().right;
        GameSettings.viewStartY = frame.getInsets().top;
        
        new SoundPlayer(GameSettings.startAudio).start();
    }
    
    public static void initGlobalData() throws IOException
    {
        //add enemyTankTypeVector
        try {
            Global.enemyTankTypeVector.add(ImageIO.read(new File(GameSettings.enemyTankType1)));
        } catch (IOException ex) {
            Logger.getLogger(GameSettings.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            Global.enemyTankTypeVector.add(ImageIO.read(new File(GameSettings.enemyTankType2)));
        } catch (IOException ex) {
            Logger.getLogger(GameSettings.class.getName()).log(Level.SEVERE, null, ex);
        }

        //add myTankTypeVector
        try {
            Global.myTankTypeVector.add(ImageIO.read(new File(GameSettings.myTankType1)));
        } catch (IOException ex) {
            Logger.getLogger(GameSettings.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            Global.myTankTypeVector.add(ImageIO.read(new File(GameSettings.myTankType2)));
        } catch (IOException ex) {
            Logger.getLogger(GameSettings.class.getName()).log(Level.SEVERE, null, ex);
        }
    
        //add mapBackgroundTypeVector
        try {
            Global.mapBackgroundTypeVector.add(ImageIO.read(new File(GameSettings.mapBackgroundType1)));
        } catch (IOException ex) {
            Logger.getLogger(GameSettings.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            Global.mapBackgroundTypeVector.add(ImageIO.read(new File(GameSettings.mapBackgroundType2)));
        } catch (IOException ex) {
            Logger.getLogger(GameSettings.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        //add wallTypeVector
        try {
            Global.wallTypeVector.add(ImageIO.read(new File(GameSettings.wallType1)));
        } catch (IOException ex) {
            Logger.getLogger(GameSettings.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        //add mapLevelVector
        Global.mapLevelVector.add(GameSettings.mapLevel1);
        Global.mapLevelVector.add(GameSettings.mapLevel2);
          
        //add myTankDefaultLocationVector
        Global.myTankDefaultLocationVector.add(new Point(150, 50));
        Global.myTankDefaultLocationVector.add(new Point(150, 50));
        
        //init myTank default position
        Global.myTank.setX(Global.myTankDefaultLocationVector.get(Global.myTankType).x);
        Global.myTank.setY(Global.myTankDefaultLocationVector.get(Global.myTankType).y);
        
        //add bomb effect
       try {
            Global.bombEffectVector.add(ImageIO.read(new File(GameSettings.bomb1)));
        } catch (IOException ex) {
            Logger.getLogger(GameSettings.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            Global.bombEffectVector.add(ImageIO.read(new File(GameSettings.bomb2)));
        } catch (IOException ex) {
            Logger.getLogger(GameSettings.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            Global.bombEffectVector.add(ImageIO.read(new File(GameSettings.bomb3)));
        } catch (IOException ex) {
            Logger.getLogger(GameSettings.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
